package com.yy.wga.user.service.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yy.wga.common.core.dto.BaseFeignApiReqDTO;
import com.yy.wga.common.core.dto.Result;
import com.yy.wga.common.util.common.StringUtils;
import com.yy.wga.user.feign.api.UserFeign;
import com.yy.wga.user.feign.request.*;
import com.yy.wga.user.feign.response.*;
import com.yy.wga.user.service.manager.UserManager;
import com.yy.wga.user.service.pojo.UserBaseDO;
import com.yy.wga.user.service.service.UserBaseService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户管理
 *
 * @author <a href="mailto:ludezh@dingtalk.com"> ludezh </a>
 * @version 1.0.0   2020/6/5,11:30
 * @since 1.0.0     2020/6/5,11:30
 */
@RestController
@RequestMapping(value = "/api/user/v1/")
public class UserController implements UserFeign {
    @Resource
    private UserBaseService userBaseService;
    @Resource
    private UserManager userManager;

    @Override
    @PostMapping("authCode2Session")
    public Result<WxAuthCode2SessionResp> authCode2Session(@Validated @RequestBody WxAuthCode2SessionReq request) {
        return Result.success(userManager.authCode2Session(request));
    }

    @Override
    @PostMapping("getWxUserInfo")
    public Result<WxGetUserInfoResp> getWxUserInfo(@Validated @RequestBody WxGetUserInfoReq request) {
        return Result.success(userManager.getWxUserInfo(request));
    }

    @Override
    @PostMapping("saveUserInfo")
    public Result<String> saveUserInfo(@Validated @RequestBody SaveUserInfoReq request) {
        if (StringUtils.isBlank(request.getAvatar())
                && StringUtils.isBlank(request.getUserName())) {
            return Result.fail("请输入修改的内容");
        }
        UserBaseDO userBaseDO = new UserBaseDO();
        userBaseDO.setId(request.getUserId());
        userBaseDO.setNickName(request.getUserName());
        userBaseDO.setAvatarUrl(request.getAvatar());
        userBaseService.updateById(userBaseDO);
        return Result.success();
    }

    @Override
    @PostMapping("saveUserAddress")
    public Result<String> saveUserAddress(@Validated @RequestBody SaveUserAddressReq request) {
        userBaseService.saveUserAddress(request);
        return Result.success();
    }

    @Override
    @GetMapping("getUserAddress")
    public Result<UserAddressResp> getUserAddress(@Validated @RequestBody BaseFeignApiReqDTO request) {
        return Result.success(userBaseService.getUserAddress(request));
    }

    @Override
    @GetMapping("getUserAddressByLngLat")
    public Result<UserAddressResp> getUserAddressByLngLat(@Validated @RequestBody UserAddressByLngLatReq request) {
        return Result.success(userBaseService.getUserAddressByLngLat(request));
    }

    // todo 放到MQ
    @Override
    @PostMapping("logout")
    public Result<Boolean> logout() {
        userManager.logout();
        return Result.success(Boolean.TRUE);
    }

    // todo 放到MQ
    @Override
    @PostMapping("modifyPhone/sendVerifyOldPhoneCode")
    public Result<String> sendVerifyOldPhoneCode() {
        userManager.sendVerifyOldPhoneCode();
        return Result.success();
    }

    // todo 放到MQ
    @Override
    @PostMapping("modifyPhone/verifyOldPhoneCode")
    public Result<String> verifyOldPhoneCode(@Validated @RequestBody VerifyOldPhoneCodeReq request) {
        userManager.verifyOldPhoneCode(request);
        return Result.success();
    }

    // todo 放到MQ
    @Override
    @PostMapping("sendModifyPhoneCode/newPhone")
    public Result<String> sendModifyPhoneCode(@Validated @RequestBody SendModifyPhoneNumberReq request) {
        userManager.sendModifyPhoneCode(request);
        return Result.success();
    }

    // todo 放到MQ
    @Override
    @PostMapping("modifyPhoneNumber")
    public Result<String> modifyPhoneNumber(@Validated @RequestBody ModifyPhoneNumberReq request) {
        userManager.modifyPhoneNumber(request);
        return Result.success();
    }

    // todo 部分放到MQ
    @Override
    @PostMapping("saveWxNum")
    public Result<QueryAccountResp> saveWxNum(@Validated @RequestBody SaveWxNumReq request) {
        return userManager.saveWxNum(request);
    }

    // todo 放到MQ
    @Override
    @PostMapping("queryAccount")
    public Result<QueryAccountResp> queryAccount() {
        return Result.success(BeansUtils.map(RequestUserHolder.getUserCache(), QueryAccountResp.class));
    }

    // todo 放到MQ
    @Override
    @PostMapping("authPhoneNumber")
    public Result<WxGetPhoneNumberResp> authPhoneNumber(@Validated @RequestBody WxGetPhoneNumberReq request) {
        return Result.success(userManager.authPhoneNumber(request));
    }

    @Override
    @PostMapping("lockFans")
    public Result<Boolean> authPhoneNumber(@Validated @RequestBody LockFansReq request) {
        if (request.getParentUserId() == null && StringUtils.isBlank(request.getInviteCode())) {
            return Result.fail("参数错误");
        }
        userManager.lockFans(request);
        return Result.success(Boolean.TRUE);
    }

    @Override
    @PostMapping("setWxNum")
    public Result<Boolean> setWxNum(@Validated @RequestBody UserWxNumberSetReq request) {
        userBaseService.update(new LambdaUpdateWrapper<UserBaseDO>()
                .eq(UserBaseDO::getAppKey, request.getAppKey())
                .eq(UserBaseDO::getId, request.getUserId())
                .set(UserBaseDO::getWxNum, request.getWxNum()));
        return Result.success(Boolean.TRUE);
    }

    @Override
    @PostMapping("getServerWxNum")
    public Result<UserServerWxNumberGetResp> getServerWxNum() {
        return Result.success(userManager.getServerWxNum());
    }

    @Override
    @PostMapping("getParentInfo")
    public Result<UserParentInfoGetResp> getParentInfo() {
        return Result.success(userManager.getParentInfo());
    }

    @Override
    @PostMapping("setRegisterBindStatus")
    public Result<QueryAccountResp> setRegisterBindStatus(@Validated @RequestBody UserRegisterAndBindStatusSetReq request) {
        return userManager.setRegisterBindStatus(request);
    }

    @Override
    @PostMapping("setUserWechatInfo")
    public Result<QueryAccountResp> setUserWechatInfo(@Validated @RequestBody UserWechatInfoSetReq request) {
        return userManager.setUserWechatInfo(request);
    }

    // todo mq
    @Override
    @PostMapping("SendVerificationCode")
    public Result<String> sendVerificationCode(@Validated @RequestBody SendVerificationCodeReq request) {
        userManager.sendVerificationCode(request);
        return Result.success();
    }

    // todo mq
    @Override
    @PostMapping("aliH5PayLogin")
    public Result<AliH5PayLoginResp> aliH5PayLogin(@Validated @RequestBody AliH5PayLoginReq request) {
        return Result.success(userManager.aliH5PayLogin(request));
    }
}
